package odgs.transaction;

import java.util.ArrayList;
import java.util.List;

import odgs.Connection;
import odgs.ContentsFile;
import odgs.SelectGridNode;
import odgs.Transaction;
import odgs.listener.FileListener;

public class FileTransaction extends Transaction
{
	public static final int SEND = 0;
	public static final int RECEIVE = 1;
	
	private Connection con;
	private int mode;
	private String name;
	private List<FileListener> fileListeners = new ArrayList<FileListener>();
	private long sendedSize;
	private long totalSize;
	private long start;
	private long end;
	private boolean isSending = false;
	private long totalTransactionID;
	private int speed = 0;
	private SelectGridNode selectGridNode;

	public FileTransaction(long id, String name, SelectGridNode node, int mode, Connection con)
	{
		super(id);
		this.name = name;
	    this.totalSize = node.getEnd() - node.getStart();
	    this.selectGridNode = node;
	    this.mode = mode;
	    this.speed = 0;
	    this.con = con;
	}
	
	public FileTransaction(int id, String name, long totalSize, int mode)
	{
		super(id);
		this.name = name;
	    this.totalSize = totalSize;
	    this.mode = mode;
	    this.speed = 0;
	}

	public FileTransaction(long id, ContentsFile file, int mode)
	{
		super(id);
		this.name = file.toString();
		this.totalSize = file.getEnd() - file.getStart();
		this.start = file.getStart();
		this.end = file.getEnd();
		this.mode = mode;
		this.speed = 0;
	}

	public void notifyEndEvnet()
	{
		for(FileListener listener : fileListeners)
		{
			listener.end(requestId, name);
		}
	}

	private void notifySendedEvent(long size)
	{
		for(FileListener listener : fileListeners)
		{
			listener.sendedSize(sendedSize, size);
		}
	}

	public void notifyStartedEvent()
	{
		for(FileListener listener : fileListeners)
		{
			listener.start(requestId, name);
		}
	}

	public void addFileListener(FileListener listener)
	{
		fileListeners.add(listener);
	}

	@Override
	public String getName()
	{
		return "FileTransaction";
	}

	@Override
	public boolean isFileTrans()
	{
		return true;
	}
	
	public long getSendedSize()
	{
		return sendedSize;
	}
	
	public long getTotalSize()
	{
		return totalSize;
	}

	public void setSelectGridNode(SelectGridNode node)
	{
		this.selectGridNode = node;
		this.start = node.getStart();
		this.end = node.getEnd();
		this.totalSize = node.getEnd() - node.getStart();
	}

	public SelectGridNode getSelectGridNode()
	{
		return selectGridNode;
	}

	public void sended(int size) 
	{
		speed += size;
		sendedSize+= size;
		notifySendedEvent(size);
	}
	
	public void setZero()
	{
		speed = 0;
	}
	
	public int getSpeed()
	{
		return speed;
	}
	
	public int getMode() {
		return mode;
	}

	public void setTotalTranscationID(long requestId) {
		totalTransactionID = requestId;
	}

	public long getTotalTranscationID() {
		return totalTransactionID;
	}
	
	@Override
	public String toString()
	{
		return "File Sending : " + name;
	}

	public long getEnd() {
		return end;
	}

	public long getStart() {
		return start;
	}

	public void cancel()
	{
		con.close();
	}
}